var lw = {};

lw.focus = function(ele, shijian) { // 焦点图
    var oUl = ele.getElementsByTagName('ul')[0],
        aLiUl = oUl.getElementsByTagName('li'),
        oOl = ele.getElementsByTagName('ol')[0],
        aLiOl = oOl.getElementsByTagName('li'),
        animated = true, // 定义表示判断是否滑动完成
        n = 1,
        aIconLen = aLiOl.length,
        width = aLiUl[0].offsetWidth,
        autoTimer = null;

    var li1 = aLiUl[0].cloneNode(true);
    oUl.appendChild(li1);

    var li2 = aLiUl[aIconLen - 1].cloneNode(true);
    oUl.appendChild(li2);

    oUl.insertBefore(li2, aLiUl[0]);

    var aImgLen = aLiUl.length;

    oUl.style.position = 'absolute';

    oUl.style.width = width * aImgLen + 'px'; // 求出ul的寬度
    for (var i = 0; i < aImgLen; i++) {
        aLiUl[i].style.width = oUl.offsetWidth / aImgLen + 'px'; // 因为每个li设置了 winth 为 100%， 如果不除以 aImgLen 则 每个li等于ul的宽度
    }
    oUl.style.left = -width + 'px';

    function showButton() { // 小按钮函数
        for (var j = 0; j < aIconLen; j++) {
            aLiOl[j].className = '';
        }
        aLiOl[n - 1].className = 'active';
    }

    function animate(offset) { // 运动函数
        animated = false;
        var newLeft = oUl.offsetLeft + offset; // 得出目标值
        // var speed = offset / 10;  匀速运动
        function go() {
            var speed = (newLeft - oUl.offsetLeft) / 10; // 缓冲运动
            speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed); // 速度一定要取整
            if (newLeft === oUl.offsetLeft) {
                animated = true;
                if (newLeft < -width * aIconLen) {
                    oUl.style.left = -width + 'px';
                }
                if (newLeft > -width) {
                    oUl.style.left = -width * aIconLen + 'px';
                }
            } else {
                oUl.style.left = oUl.offsetLeft + speed + 'px';
                setTimeout(go, 10);
            }
        }
        go();
    }
    for (var j = 0; j < aIconLen; j++) {
        aLiOl[j].index = (j + 1);
        aLiOl[j].onclick = function() {
            if (!animated) {
                return;
            }
            if (this.className === 'active') {
                return;
            }
            var myIndex = this.index;
            var offset = -width * (myIndex - n);
            n = myIndex;
            animate(offset);
            showButton();
        };
    }

    var hammertime = new Hammer(ele); // 新建 hammertime 对象
    hammertime.on('swipeleft', function(e) {
        if (!animated) {
            return;
        }
        if (n === aIconLen) {
            n = 1;
        } else {
            n += 1;
        }
        animate(-width);
        showButton();
    });
    hammertime.on('swiperight', function(e) {
        if (!animated) {
            return;
        }
        if (n === 1) {
            n = aIconLen;
        } else {
            n -= 1;
        }
        animate(width);
        showButton();
    });

    function play() {
        autoTimer = setInterval(function() {
            if (!animated) {
                return;
            }
            if (n === aIconLen) {
                n = 1;
            } else {
                n += 1;
            }
            animate(-width);
            showButton();
        }, shijian);
    }
    play();
};

lw.getTop = function(obj) { // 返回一个元素距离页面顶端的距离
    var iTop = 0;
    while (obj) {
        iTop += obj.offsetTop; // 累加
        obj = obj.offsetParent; // obj 的父元素
    }
    return iTop;
};

lw.loadImage = function(Img) { // 图片按需加载
    var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
    for (var i = 0; i < Img.length; i++) {
        if (!Img[i].isLoad && lw.getTop(Img[i]) < (scrollTop + document.documentElement.clientHeight)) {
            Img[i].isLoad = true;
            Img[i].src = Img[i].getAttribute('_src') || Img[i].src;
        }
    }
};

lw.youhua = function(obj, src) { // 图片预加载

    var all_img = obj.getElementsByTagName('img');

    if (all_img.length == 0) {
        return false;
    }

    var new_arr_img = [];

    for (var i = 0; i < all_img.length; i++) {
        new_arr_img.push(all_img[i].src);
        all_img[i].src = src; // 初始化状态全部图片， 统一替换成单一图片
    }

    var iCur = 0;

    function yujiazai() { // 所有图片预加载， 更换图片

        all_img[iCur].src = new_arr_img[iCur];
        all_img[iCur].onload = xiazai;
        all_img[iCur].onerror = xiazai;

        function xiazai() {
            iCur += 1;
            if (iCur < new_arr_img.length) {
                yujiazai();
            } else {
                return false;
            }
        }
    }
    yujiazai();
};

lw.imgload = function() { // 图片按需加载，图片预加载

    var Aimg = document.getElementsByTagName('img');
    lw.loadImage(Aimg);
    window.addEventListener('resize', function() {
        lw.loadImage(Aimg);
    });
    window.addEventListener('scroll', function() {
        lw.loadImage(Aimg);
    });

};

lw.tabSwitch = function(oParent, oChild, aTab, oClass) {
    var aBtn = oParent.getElementsByClassName(oChild),
        aIndex_tab = document.getElementsByClassName(aTab);
    for (var i = 0; i < aBtn.length; i++) {
        aBtn[i].index = i;
        aBtn[i].onclick = function() {
            for (var i = 0; i < aBtn.length; i++) {
                aBtn[i].className = oClass;
                aIndex_tab[i].style.display = 'none';
            }
            this.className += ' active';
            aIndex_tab[this.index].style.display = 'block';
        };
    }
};

function star_grade(boxItem) { // 评价星星

    var starItem = $(boxItem);
    var input    = $(boxItem).find('input');

    for (var i = 0; i < starItem.length; i++) {
        (function(i) {
            var star = starItem[i].getElementsByTagName("span");
            for (var j = 0; j < star.length; j++) {
                (function(j) {
                    star[j].onclick = function() {
                         show(star, j);
                         lock(i, j);
                    };
                })(j);
            }
        })(i);
    }

    function show(o, k) {
        for (var i = 0; i < o.length; i++) {
            o[i].className = "";
            if (i <= k) o[i].className = "show";
        }
    }

    function lock(x, k) {
        input[x].value = k + 1;
    }
}
